package com.wmyg.antifake.codecreator;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * @author 王卓逸
 * @program TODO
 * @description TODO
 * @create 2019-05-08
 */
public class CodeCreator<F, R, D extends Dozen> {

    public Collection<D> createDozens(F foundation, DozenCreator<F, D> creator) {
        return creator.create(foundation);
    }

    public R createCodeAndOperations(Collection<D> dozens, CodeRule codeRule,
                                               AsyncOperation asyncOperation,
                                               SyncOperation<R, D> syncOperation){
        List<CompletableFuture<D>> dozenFutures = dozens.stream().map(d ->
                CompletableFuture.supplyAsync(() -> {

                    asyncOperation.operateForaDozen(d);
                    return d;
                })).collect(Collectors.toList());
        Map<String, List<D>> dozenMap = dozenFutures.stream().map(CompletableFuture::join).collect(Collectors.groupingBy(Dozen::getPackLevel));
        return syncOperation.operateForDozenMap(dozenMap);
    }

}
